//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.eshop.modules.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.eshop.common.service.impl.BaseServiceImpl;
import com.eshop.enums.ShopCommonEnum;
import com.eshop.modules.shop.domain.SystemUserLevel;
import com.eshop.modules.user.domain.ShopUser;
import com.eshop.modules.user.domain.UserLevel;
import com.eshop.modules.user.service.SystemUserLevelService;
import com.eshop.modules.user.service.SystemUserTaskService;
import com.eshop.modules.user.service.UserLevelService;
import com.eshop.modules.user.service.UserService;
import com.eshop.modules.user.service.mapper.SystemUserTaskMapper;
import com.eshop.modules.user.service.mapper.UserLevelMapper;
import com.eshop.utils.OrderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(
        rollbackFor = {Exception.class}
)
public class UserLevelServiceImpl extends BaseServiceImpl<UserLevelMapper, UserLevel> implements UserLevelService {
    private static final Logger log = LoggerFactory.getLogger(UserLevelServiceImpl.class);
    @Autowired
    private UserLevelMapper userLevelMapper;
    @Autowired
    private SystemUserTaskMapper yxSystemUserTaskMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private SystemUserLevelService systemUserLevelService;
    @Autowired
    private SystemUserTaskService systemUserTaskService;

    public UserLevelServiceImpl() {
    }

    public boolean setLevelComplete(Long uid) {
        int levelId = 0;
        UserLevel userLevel = this.getUserLevel(uid, (Integer)null);
        if (userLevel != null) {
            levelId = userLevel.getLevelId();
        }

        int nextLevelId = this.systemUserLevelService.getNextLevelId(levelId);
        if (nextLevelId == 0) {
            return false;
        } else {
            int finishCount = this.systemUserTaskService.getTaskComplete(nextLevelId, uid);
            if (finishCount == 3) {
                this.setUserLevel(uid, nextLevelId);
                return true;
            } else {
                return false;
            }
        }
    }

    public UserLevel getUserLevel(Long uid, Integer grade) {
        LambdaQueryWrapper<UserLevel> wrapper = new LambdaQueryWrapper();
        ((wrapper.eq(UserLevel::getStatus, ShopCommonEnum.IS_STATUS_1.getValue())).eq(UserLevel::getUid, uid)).orderByDesc(UserLevel::getGrade);
        if (grade != null) {
            wrapper.lt(UserLevel::getGrade, grade);
        }

        UserLevel userLevel = (UserLevel)this.getOne(wrapper, false);
        if (ObjectUtil.isNull(userLevel)) {
            return null;
        } else if (ShopCommonEnum.IS_FOREVER_1.getValue().equals(userLevel.getIsForever())) {
            return userLevel;
        } else {
            int nowTime = OrderUtil.getSecondTimestampTwo();
            if (nowTime > userLevel.getValidTime()) {
                if (ShopCommonEnum.IS_STATUS_1.getValue().equals(userLevel.getStatus())) {
                    userLevel.setStatus(ShopCommonEnum.IS_STATUS_0.getValue());
                    this.userLevelMapper.updateById(userLevel);
                }

                return this.getUserLevel(uid, userLevel.getGrade());
            } else {
                return userLevel;
            }
        }
    }

    private void setUserLevel(Long uid, int levelId) {
        SystemUserLevel systemUserLevelQueryVo = (SystemUserLevel)this.systemUserLevelService.getById(levelId);
        if (!ObjectUtil.isNull(systemUserLevelQueryVo)) {
            int validTime = systemUserLevelQueryVo.getValidDate() * 86400;
            LambdaQueryWrapper<UserLevel> wrapper = new LambdaQueryWrapper();
            (((wrapper.eq(UserLevel::getStatus, ShopCommonEnum.IS_STATUS_1.getValue())).eq(UserLevel::getUid, uid)).eq(UserLevel::getLevelId, levelId)).last("limit 1");
            UserLevel userLevel = new UserLevel();
            userLevel.setIsForever(systemUserLevelQueryVo.getIsForever());
            userLevel.setStatus(ShopCommonEnum.IS_STATUS_1.getValue());
            userLevel.setGrade(systemUserLevelQueryVo.getGrade());
            userLevel.setUid(uid);
            userLevel.setLevelId(levelId);
            userLevel.setDiscount(systemUserLevelQueryVo.getDiscount().intValue());
            if (ShopCommonEnum.IS_FOREVER_1.getValue().equals(systemUserLevelQueryVo.getIsForever())) {
                userLevel.setValidTime(0);
            } else {
                userLevel.setValidTime(validTime + OrderUtil.getSecondTimestampTwo());
            }

            userLevel.setMark("恭喜你成为了" + systemUserLevelQueryVo.getName());
            this.userLevelMapper.insert(userLevel);
            ShopUser shopUser = new ShopUser();
            shopUser.setLevel(levelId);
            shopUser.setUid(uid);
            this.userService.updateById(shopUser);
        }
    }
}
